package tech.yangzhijie.basic.knowledge.other;

import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
import java.util.concurrent.locks.Condition;

public class Demo {


    @Test
    public void 字符型常量和字符串常量区别(){
        /*
            字符型常量，单引号括起来的内容，占用2字节，可参与表达式运算
            字符串常量，双引号括起来的内容，占用若干字节，不可参与表达式运算
         */
        char c1 = 'a';
        System.out.println(c1);
        String s1 = "aaa";
        System.out.println(s1);
    }

    @Test
    public void 值传递(){
        /*
         *  传递的是引用对象的拷贝，如果你将拷贝指向另一个对象对原来的引用对象没用
         *  传递基本类型引用也是传递的拷贝，改变他的值也对原来的基本类型引用没用
         */
        int[] arr = {5,4,3,2,1};
        System.out.println("值传递前：arr");
        for (int i:arr){
            System.out.print(i + "\t");
        }
        int[] arr2 = 值传递测试(arr);
        System.out.println("\n值传递后：arr");
        for (int i:arr){
            System.out.print(i + "\t");
        }
        System.out.println("\narr2");
        for (int i:arr2){
            System.out.print(i + "\t");
        }
    }

    public int[] 值传递测试(int[] arr){

        arr = new int[]{1,2,3,4,5};
        Map<String,String> concurrentHashMap = new ConcurrentHashMap();
        Map<String,String> hashMap = new HashMap();

        String String = "evict";
        return arr;
    }

    @Test
    public void Test(){

    }

    @Test
    public void testArrayList(){
        List<String> list = new CopyOnWriteArrayList<>();
        CountDownLatch countDownLatch = new CountDownLatch(30);
        for (int i = 0; i < 30; i++) {
            new Thread(() -> {
                list.add(UUID.randomUUID().toString().substring(0, 8));
                System.out.println(list);
                countDownLatch.countDown();
            }, String.valueOf(i)).start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static String MD5(String text, String charset) {
        char[] DIGITS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        MessageDigest msgDigest = null;
        try {
            msgDigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException var6) {
            throw new IllegalStateException("System doesn't support MD5 algorithm.");
        }
        try {
            msgDigest.update(text.getBytes(charset));
        } catch (UnsupportedEncodingException var5) {
            throw new IllegalStateException("System doesn't support your  EncodingException.");
        }

        byte[] bytes = msgDigest.digest();
        int l = bytes.length;
        char[] out = new char[l << 1];
        int i = 0;

        for(int j = 0; i < l; ++i) {
            out[j++] = DIGITS[(240 & bytes[i]) >>> 4];
            out[j++] = DIGITS[15 & bytes[i]];
        }
        String md5Str = new String(out);
        return md5Str;
    }

}
